package com.zmhz.intercept;

import com.eova.aop.AopContext;
import com.eova.aop.MetaObjectIntercept;
import com.eova.common.Easy;
import com.eova.common.utils.xx;
import com.eova.common.utils.time.DateUtil;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

public class CorporateInfoInterceptor extends MetaObjectIntercept {

	/**
	 * 添加用户企业信息后修改用户信息的状态为会员用户
	 */
	@Override
	public String addAfter(AopContext ac) throws Exception {
		Integer userid = ac.record.getInt("userid");

		String sql = "select usertype,regtime from frontusers  where id=?";
		Record record = Db.findFirst(sql, userid);
		long days = DateUtil.getIntevalDays(DateUtil.getCurrDate(), record.getDate("regtime"));
		// 当免费会员超过180天，会员状态设为“2=会员过期”;===后期此处应该加上收费会员的判断===
		if (days > 180) {
			sql = "update frontusers set userstatus=2 where id=?";
			Db.update(sql, userid);
		} else {
			String province = ac.record.getStr("province");
			String city = ac.record.getStr("city");
			String region = ac.record.getStr("region");
			String address = ac.record.getStr("address");
			// 当必要的企业信息填写好后，可以更新用户表的用户状态
			if (null != province && null != city && null != region && null != address) {
				sql = "select name,province ,city ,region ,address  from users_info  where userid=?";
				record = Db.findFirst(sql, userid);
				if(!xx.isEmpty(record)) {
					String name = record.getStr("name");
					province = record.getStr("province");
					city = record.getStr("city");
					region = record.getStr("region");
					address = record.getStr("address");
					if (null != name && null != province && null != city && null != region && null != address) {
						sql = "update frontusers set userstatus=1 where id=?";
						Db.update(sql, userid);
					}
				}
			}
		}

		return null;
	}

	@Override
	public String addSucceed(AopContext ac) throws Exception {
		Integer userid = ac.records.get(0).getInt("userid");
		String sql = "select token,userstatus,usertype from frontusers  where id=?";
		Record record = Db.findFirst(sql, userid);
		Easy easy = new Easy();
		if (!xx.isEmpty(record)) {
			easy.setData(record);
			easy.setSuccess(true);
			easy.setMsg("操作成功");
		} else {
			easy.setData(null);
			easy.setSuccess(false);
			easy.setMsg("操作失败");
		}

		return JsonKit.toJson(easy);
	}

	@Override
	public String updateBefore(AopContext ac) throws Exception {
		Integer userid = ac.record.getInt("userid");

		String sql = "select count(*) from corporate_info as a1 " + " left join frontusers as a2 on a1.userid=a2.id "
				+ " where a2.id=?";

		Integer count = Db.queryInt(sql, userid);
		// 修改企业信息时企业信息不存在先添加企业信息
		if (count == 0) {
			Db.save("corporate_info", ac.record);
		}
		return null;
	}

	@Override
	public String updateAfter(AopContext ac) throws Exception {
		Integer userid = ac.record.getInt("userid");

		String sql = "select usertype,regtime from frontusers  where id=?";
		Record record = Db.findFirst(sql, userid);
		long days = DateUtil.getIntevalDays(DateUtil.getCurrDate(), record.getDate("regtime"));
		// 当免费会员超过180天，会员状态设为“2=会员过期”;===后期此处应该加上收费会员的判断===
		if (days > 180) {
			sql = "update frontusers set userstatus=2 where id=?";
			Db.update(sql, userid);
		} else {
			sql = "select province,city,region,address from corporate_info  where userid=?";
			record = Db.findFirst(sql, userid);
			String province = ac.record.getStr("province");
			String city = ac.record.getStr("city");
			String region = ac.record.getStr("region");
			String address = ac.record.getStr("address");
			// 当必要的企业信息填写好后，可以更新用户表的用户状态
			if (null != province && null != city && null != region && null != address) {
				sql = "select name,province ,city ,region ,address  from users_info  where userid=?";
				record = Db.findFirst(sql, userid);
				if(!xx.isEmpty(record)) {
					String name = record.getStr("name");
					province = record.getStr("province");
					city = record.getStr("city");
					region = record.getStr("region");
					address = record.getStr("address");
					if (null != name && null != province && null != city && null != region && null != address) {
						sql = "update frontusers set userstatus=1 where id=?";
						Db.update(sql, userid);
					}
				}
			}
		}

		return null;
	}

	@Override
	public String updateSucceed(AopContext ac) throws Exception {
		Integer userid = ac.records.get(0).getInt("userid");
		String sql = "select token,userstatus,usertype from frontusers  where id=?";
		Record record = Db.findFirst(sql, userid);
		Easy easy = new Easy();
		if (!xx.isEmpty(record)) {
			easy.setData(record);
			easy.setSuccess(true);
			easy.setMsg("操作成功");
		} else {
			easy.setData(null);
			easy.setSuccess(false);
			easy.setMsg("操作失败");
		}

		return JsonKit.toJson(easy);
	}

	public void queryAfter(AopContext ac) throws Exception {
		if (!xx.isEmpty(ac.record)&&!xx.isEmpty(ac.record.getColumns())) {
			Integer id = ac.record.getInt("id");
			String sql = "select province provincecode,city citycode,region regioncode,busrange,cotype,comsize,cestatus from corporate_info  where id=?";
			Record record = Db.findFirst(sql, id);
			ac.record.set("provincecode", record.get("provincecode"));
			ac.record.set("citycode", record.get("citycode"));
			ac.record.set("regioncode", record.get("regioncode"));
			ac.record.set("busrange", record.get("busrange"));
			ac.record.set("cotype", record.get("cotype"));
			ac.record.set("comsize", record.get("comsize"));
			ac.record.set("cestatus", record.get("cestatus"));

			//

		}
	}
}
